//
//  QAuthViewController.swift
//  swiftTest
//
//  Created by LiTing on 17/6/15.
//  Copyright © 2017年 LiTing. All rights reserved.
//

import UIKit
import SVProgressHUD

class QAuthViewController: UIViewController {
    //MARK:- 控件的属性
    @IBOutlet weak var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        //1.设置导航栏的内容
        setupNavigationBar()
        
        //2.加载网页
        loadPage()
    }

   
}

//MARK:- 设置UI界面相关
extension QAuthViewController {
    private func setupNavigationBar() {
       //1.设置左侧的item
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭",style: .Plain, target: self, action: #selector(QAuthViewController.closeItemClick))
        
        //2.设置右侧item
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "填充", style: .Plain, target: self, action: #selector(QAuthViewController.fillItemClick))
        
        //3.设置标题
        title = "登录界面"
    }
    
    private func loadPage() {
        //1.获取登录页面的URLstring
        let urlString = "https://api.weibo.com/oauth2/authorize?client_id=\(app_key)&redirect_uri=\(redirect_uri)"
        
        //2.创建对应NSURL
        guard let url = NSURL(string: urlString) else{
            return
        }
        
        //3.创建NSURLRequest对象
        let request = NSURLRequest(URL: url)
        
        //4.加载request对象
        webView.loadRequest(request)
    }
}

//MARK:- 事件监听函数
extension QAuthViewController {
    @objc private func closeItemClick() {
         dismissViewControllerAnimated(true, completion: nil)
    }
    
    @objc private func fillItemClick() {
        //1.书写js代码
        let jsCode = "document.getElementById('userId').value='2058914895@qq.com';document.getElementById('passwd').value='Lt321789';"
        
        //2.执行js代码
        webView.stringByEvaluatingJavaScriptFromString(jsCode)
    }
}

//MARK:- webview的delegate方法
extension QAuthViewController : UIWebViewDelegate {
    //webView开始加载网页
    func webViewDidStartLoad(webView: UIWebView) {
        SVProgressHUD.show()
    }
    
    //webView网页加载完成
    func webViewDidFinishLoad(webView: UIWebView) {
        SVProgressHUD.dismiss()
    }
    
    //webView网页加载失败
    func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
        SVProgressHUD.dismiss()
    }
    
    //当准备加载某一个页面时，会执行该方法
    //返回值：true -继续加载该页面  false -不会加载该页面
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        //1.获取加载网页的nsurl
        guard let url = request.URL else {
            return true
        }
        
        //2.获取URL中的字符串
        let urlString = url.absoluteString
        
        //3.判断该字符串中是否包含code
        guard urlString.containsString("code=") else {
            return true
        }
        
        //4.将code截取出来
        let code = urlString.componentsSeparatedByString("code=").last!
        
        //5.请求accesstoken
        loadAccessToken(code)
        
        return false
    }

}


//MARK:- 请求数据
extension QAuthViewController{
    ///请求accesstoken
    private func loadAccessToken(code : String) {
        NetworkTools.shareInstance.loadAccessToken(code) { (result, error) in
            //1.错误效验
            if error != nil {
                print(error)
                return
            }
            
            //2.拿到结果
            guard let accountDict = result else{
                print("没有获取授权后的数据")
                return
            }
            
            //3.将字典转成模型对象
            let account = UserAccount(dict: accountDict)
            
            //4.请求用户信息
            self.loadUserInfo(account)

        }
    }
    
    private func loadUserInfo(account : UserAccount) {
        //1.获取accesstoken
        guard let accessToken = account.access_token else {
            return
        }
        
        //2.获取uid
        guard let uid = account.uid else {
            return
        }
        
        //3.发送网络请求
        NetworkTools.shareInstance.loadUserInfo(accessToken, uid: uid) { (result, error) in
            //1.错误效验
            if error != nil {
                print(error)
                return
            }
            
            //2.拿到用户信息结果
            guard let userInfoDict = result else {
                return
            }
            
            //3.从字典中取出昵称和用户头像地址
            account.screen_name = userInfoDict["screen_name"] as? String
            account.avatar_large = userInfoDict["avatar_large"] as? String
            
            print(account)
            
            
        }
        
    }
}